package io.github.shenbinglife.springboot2.layui.admin.controller;

import io.github.shenbinglife.springboot2.layui.admin.entity.Org;
import io.github.shenbinglife.springboot2.layui.admin.entity.User;
import io.github.shenbinglife.springboot2.layui.admin.service.impl.OrgService;
import io.github.shenbinglife.springboot2.layui.common.model.Result;
import io.github.shenbinglife.springboot2.layui.common.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("admin/orgs")
@RestController
public class OrgController {

    @Autowired
    private OrgService orgService;

    @GetMapping("{id}/tree")
    public Result<List<TreeNode<Long>>> tree(@PathVariable("id") Long parentId, @RequestParam(defaultValue = "true") Boolean recursive) {
        List<TreeNode<Long>> tree = orgService.getOrgTree(parentId, recursive);
        return Result.success(tree);
    }
    /**
     * 根据ID获取 组织
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<Org> getOrg(@PathVariable Long id) {
        return orgService.getOptionalById(id)
                .map(Result::success)
                .orElse(Result.failure("组织不存在"));
    }

    /**
     * 更新组织
     *
     * @param org
     * @return
     */
    @PostMapping()
    public Result<Org> update(@RequestBody Org org) {
        orgService.update(org);
        return Result.success(orgService.getById(org.getId()));
    }

    /**
     * 创建组织
     *
     * @param org
     * @return
     */
    @PutMapping
    public Result<Org> create(@RequestBody Org org) {
        Org save = orgService.save(org);
        return Result.success(save);
    }

    /**
     * 删除组织
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result<Void> delete(@PathVariable Long id) {
        orgService.deleteWithChildren(id);
        return Result.success();
    }

    /**
     * 添加用户到角色
     * @param orgId
     * @param userIds
     * @return
     */
    @PostMapping("{orgId}/users")
    public Result<Void> addUsersToRole(@PathVariable("orgId") Long orgId, @RequestParam Long[] userIds) {
        orgService.addUsersToOrg(orgId, userIds);
        return Result.success();
    }

    /**
     * 移除角色中的用户
     * @param orgId
     * @param userIds
     * @return
     */
    @DeleteMapping("{orgId}/users/{userIds}")
    public Result<Void> removeUsersFromRole(@PathVariable("orgId") Long orgId, @PathVariable Long[] userIds) {
        orgService.removeUsersFromOrg(orgId, userIds);
        return Result.success();
    }

    /**
     * 查询角色的用户列表
     *
     * @param orgId
     * @return
     */
    @GetMapping("{orgId}/users")
    public Result<List<User>> getRoleUsers(@PathVariable("orgId") Long orgId) {
        List<User> users = orgService.getOrgUsers(orgId);
        return Result.success(users);
    }

    /**
     * 分页查询角色的用户列表
     *
     * @param orgId
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("{orgId}/users/page")
    public Result<Page<User>> getPageRoleUsers(@PathVariable("orgId") Long orgId,
                                               @RequestParam(required = false) String keyword,
                                               @RequestParam(defaultValue = "0") Integer page,
                                               @RequestParam(defaultValue = "20") Integer limit) {
        Page<User> users = orgService.getPageRoleUsers(orgId,keyword, page - 1, limit);
        return Result.success(users);
    }

}
